package 注解;

import org.junit.Test;

import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.sql.SQLOutput;
import java.util.Arrays;

public class AnnotationDemo {
    @Test
    public void parseClass() throws Exception {
        //1.定位class类对象,反射第一步都是拿到类对象
        Class c = BookStore.class;
        //定位方法对象
        Method run = c.getDeclaredMethod("run");
        //2.判断这个类上是否使用的某个注解

    /*
    if (c.isAnnotationPresent(Book.class))
        {
            //3.获取这个注解对象
            Book book =(Book) c.getDeclaredAnnotation(Book.class);
            System.out.println(book.value());
            System.out.println(book.price());
            System.out.println(Arrays.toString(book.authors()));
        }
    */
        //判断方法上是否使用某个注解

        if (run.isAnnotationPresent(Book.class)) {
            //3.获取这个注解对象
            Book book = (Book) run.getDeclaredAnnotation(Book.class);
            System.out.println(book.value());
            System.out.println(book.price());
            System.out.println(Arrays.toString(book.authors()));
        }
    }
}

@Book(value = "《Java基础》",price = 99,authors = "zhulei")
class BookStore{
    @Book(value = "《Java进阶》",price = 89,authors = "admin")
    public void run()
    {

    }
}


@Target({ElementType.TYPE,ElementType.METHOD})  //类和成员方法上使用
@Retention(RetentionPolicy.RUNTIME) //注解永久存活
@interface Book{
    String value();
    double price() default 100;
    String[] authors();
}